package com.hjs.servlet.login;

import com.hjs.mapper.UsersMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@WebServlet("/user/login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {

        RequestUtil requestUtil = new RequestUtil(request, response);
        Mapper<UsersMapper> usersMapperMapper = new Mapper<>(UsersMapper.class);
        try {
            UsersMapper mapper = usersMapperMapper.getMapper();
            String token = mapper.getToken(requestUtil.getString("userID"), requestUtil.getString("password"));
            if (token == null) {
                requestUtil.responseFail("用户名或密码错误");
            } else {
                requestUtil.responseWrite("{\"token\":\"" + token + "\"}");
            }

        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        } finally {
            usersMapperMapper.closeSession();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    }
}
